/**
 * @author zhengjiachao
 * @since 2017-3-27
 */
ADF.Designer = function Designer(shell, contents, bundleList) {
    this.contents = contents;
    this.shell = shell;
    this.editorContainer = shell;
    this._createContainers(shell);
    this.bundleList = bundleList || this._getBundleList();
    this.resolvedBundles = this._resolveBundles();
    this.hotkeyHolder = new AGEF.HotkeyHolder();
    var inputs = {
        'editorContainer': this._getEditorContainer(),
        'graphicalContainer': this._getGraphicalContainer(),
        'paletteContainer': this._getPaletteContainer(),
        'toolbarContainer': this._getToolbarContainer(),
        'propertyContainer': this._getPropertyContainer(),
        'outlineContainer': this._getOutlineContainer(),
        'editPartFactory': this._getEditPartFactory(),
        'widgetFactory': this._getWidgetFactory(),
        'contents': this._getContents(),
        'paletteContents': this._getPaletteContents(),
        'propertyContents': this._getPropertyContents(),
        'toolbarContents': this._getToolbarContents()
    };
    ADF.Designer.superclass.constructor.call(this, inputs);
}

COM.util.defineClass(ADF.Designer, AGEF.DefaultEditor, {

    //override
    createPaletteViewers: function (inputs) {
        var viewers = ADF.Designer.superclass.createPaletteViewers.call(this, inputs);
        viewers[0].registerControl(AGEF.ToolEntry.LABEL, ADF.ToolEntry);
        return viewers;
    },

    //override
    //@king
    createGraphicalViewers: function (inputs) {
        var graphicalViewers = ADF.Designer.superclass.createGraphicalViewers.call(this, inputs);
        var designer = this;
        if (graphicalViewers && graphicalViewers.length > 0) {
            for (var i = 0; i < graphicalViewers.length; i++) {
                var graphicalViewer = graphicalViewers[i];
                graphicalViewer.setKeyHandler({
                    keyDown: function keyDown(e) {
                        designer.hotkeyHolder.execute(e, graphicalViewer);
                        e.preventDefault();
                    },
                    keyPressed : function keyPressed(e){
                        designer.hotkeyHolder.execute(e, graphicalViewer);
                        e.preventDefault();
                    },
                    keyReleased : function keyReleased(e){
                        designer.hotkeyHolder.execute(e, graphicalViewer);
                        e.preventDefault();
                    }
                });
            }
        }
        return graphicalViewers;
    },

    _resolveBundles: function () {
        var r = {
            Action: [],
            Control: [],
            Property: [],
            Tool: []
        };
        for (var i = 0; i < this.bundleList.length; i++) {
            var bundle = this.bundleList[i];
            r[bundle.getType()].push(bundle);
        }
        return r;
    },
    //插件列表，列表返回
    _getBundleList: Function.needOverride,

    _getEditorContainer: function () {
        return this.shell;
    },

    _getGraphicalContainer: function () {
        return document.getElementById("adf_content_container");
    },

    _getPaletteContainer: function () {
        return document.getElementById("adf_palette_container");
    },

    _getOutlineContainer: function () {
        return document.getElementById("adf_outline_container");
    },

    _getPropertyContainer: function () {
        return document.getElementById("adf_property_container");
    },

    _getToolbarContainer: function () {
        return document.getElementById('adf_action_container');
    },

    _getEditPartFactory: function () {
        return new ADF.PartFactory(this.resolvedBundles['Control']);
    },


    _getWidgetFactory: function () {
        return new ADF.ControlFactory(this.resolvedBundles['Control']);
    },


    _getContents: function () {
        return this.contents;
    },

    _getPaletteContents: function () {
        var tools = this.resolvedBundles['Tool'];
        // 1. 初始化分组  2.初始化工具  3.把工具放入分组   4.把分组返回
        var contents = [];
        var groups = {};
        tools.forEach(function (tool) {
            var ins = tool.getGroupInstance();
            if (ins) {
                groups[ins['id']] = ins['instance'];
                contents.push(ins['instance']);
            }
        }, this);
        tools.forEach(function (tool) {
            var ins = tool.getToolModelInstance();
            if (ins) {
                groups[ins['id']].addToolModel(ins['instance']);
            }
        }, this);
        return contents;
    },

    _getPropertyContents: function () {
        var properties = this.resolvedBundles['Property'];
        var contents = [];
        var groups = {};
        properties.forEach(function (property) {
            var ins = property.getGroupInstance();
            if (ins) {
                contents.push(ins);
            }
        }, this);
        return contents;
    },

    _getToolbarContents: function () {
        var actions = this.resolvedBundles['Action'];
        var contents = [];
        actions.forEach(function (action) {
            var ins = action.getModelInstance();
            if (ins) {
                contents.push(ins);
                this.hotkeyHolder.registerAction(ins);
            }
        }, this);
        return contents;
    },

    _createContainers: function (shell) {
        shell.className = shell.className + " container_fluid";
        shell.innerHTML =
            '<div class="ui-layout-north" id="adf_action_container"></div>' +

            '<div class="ui-layout-west" id="adf_palette_container"></div>' +
            '<div class="ui-layout-east" style="border:none;padding: 0">'+
            '<div class="ui-layout-center" id="adf_property_container">'+
            '<div class="adf_property_container">元素属性<i class="iconfont icon-arrow"></i></div>'+
            '</div>'+
            '<div class="ui-layout-north" id="adf_outline_container">'+
            '<div class="adf_outline_container">结构<i class="iconfont icon-arrow"></div>'+
            '</div></div>'+
            '<div class="ui-layout-center" id="adf_content_container"></div>';

        var myLayout=$(shell).layout({
            spacing_open:1,
            spacing_closed:4,
            resizerTip:"可调整大小",
            sliderTip:"显示/隐藏侧边栏",
            togglerTip_open:"关闭",
            togglerTip_closed:"打开",
            togglerLength_open:0,
            togglerLength_closed:0,
            north:{
                size:40,
                minSize:20,
                maxSize:100
            },
            west:{
                size:0.17,
                minSize:115,
                maxSize:0.5
            },
            east:{
                size:0.2,
                minSize:200,
                maxSize:0.5
            },
            center:{
                minWidth:100
            }
        });
        $(".ui-layout-east").layout({
            spacing_open:1,
            spacing_closed:4,
            resizerTip:"可调整大小",
            sliderTip:"显示/隐藏侧边栏",
            togglerTip_open:"关闭",
            togglerTip_closed:"打开",
            togglerLength_open:0,
            togglerLength_closed:0,
            north:{
                size:0.5,
                minSize:100,
                maxSize:0.9
            }
        });

    },

    configurePropertyControl: function (propertyViewer) {
        ADF.Designer.superclass.configurePropertyControl.call(this, propertyViewer);
        propertyViewer.registerControl(AGEF.TextPropertyEditor.LABEL, ADF.TextPropertyEditor);
        propertyViewer.registerControl(ADF.AttrColorPropertyEditor.LABEL, ADF.AttrColorPropertyEditor);
        var properties = this.resolvedBundles['Property'];
        properties.forEach(function (property) {
            var control = property.getEditorControl();
            if (control) {
                propertyViewer.registerControl(control.getID(), control);
            }
            var modalRenderer = property.getModalRenderer();
            if (modalRenderer) {
                var $renderContainer = $('<div></div>');
                $(shell).append($renderContainer);
                modalRenderer.init($renderContainer[0]);
                ADF.registerModalRenderer(modalRenderer.getID(), modalRenderer);
            }
        }, this);
    }

});